package com.spring.dao.gf.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.spring.bean.gf.GfSort;
import com.spring.bean.page.Page;
import com.spring.bean.page.PageView;
import com.spring.dao.DaoSupport;
import com.spring.dao.gf.GfSortDao;
import com.spring.exception.DaoException;
import com.spring.utils.Constants;

@Repository
public class GfSortDaoImpl extends DaoSupport implements GfSortDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	private static final String TABLE_GF_SORT = "gf_sort";

	/** insert */
	@Override
	public boolean insert(GfSort gfSort) throws DaoException{
		try{
			return insert(jdbcTemplate, gfSort);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DaoException("GfSort --- insert error : " + e.getMessage());
		}
	}

	/** udpate */
	@Override
	public boolean update(GfSort gfSort) throws DaoException{
		try{
			return update(jdbcTemplate, gfSort);
		} catch (Exception e) {
			throw new DaoException("GfSort --- update error : " + e.getMessage());
		}
	}

	/** delete */
	@Override
	public boolean delete(Integer id) throws DaoException{
		try{
			return deleteById(jdbcTemplate, TABLE_GF_SORT, id);
		} catch (Exception e) {
			throw new DaoException("GfSort --- delete error : " + e.getMessage());
		}
	}

	/** getEntityById */
	@Override
	public GfSort getById(Integer id) throws DaoException{
		try{
			return getById(jdbcTemplate, GfSort.class , id);
		} catch (Exception e) {
			throw new DaoException("GfSort --- query by id error : " + e.getMessage());
		}
	}

	/** 
	 *  queryForList 
	 * 	_pid is null : select all parents
	 * 	else : select sort those parent is selected
	 */
	@Override
	public List<GfSort> queryForList(Integer pid) throws DaoException{
		try{
			String sql = "select id,pid,name,order_by from " + TABLE_GF_SORT + " where pid = ? and isable = ? order by order_by ";
			return queryForList(jdbcTemplate, GfSort.class, sql, pid, Constants.RECORD_ON);
		} catch (Exception e) {
			throw new DaoException("GfSort --- query list error : " + e.getMessage());
		}
	}

	/** queryForPage */
	@Override
	public PageView<GfSort> queryForPage(Page page) throws DaoException{
		String sql = "select SQL_CALC_FOUND_ROWS id,pid,name,order_by from " + TABLE_GF_SORT;
		try{
			return queryForPage(jdbcTemplate, GfSort.class, sql, page);
		} catch (Exception e) {
			throw new DaoException("GfSort --- query page error : " + e.getMessage());
		}
	}
}